#encoding=UTF-8
import os

from google.appengine.ext.webapp import template
from google.appengine.ext import db

from django.utils import simplejson as json
from inc.view import View


class MCampus(db.Model):
  json = db.TextProperty()

class Campus(View):
  def __init__(self, handler, mode, temp, path = "", s = None):
    View.__init__(self, handler, path, temp, s)
    if mode == 'post':
      key = self.GET('key')
      json = self.GET('json')
      if key:
        campus = db.get(key)
        campus.json = json
      else:
        campus = MCampus(json = json)
      campus.put()

      self.WRITE(campus.key());
    if mode == 'get':
      key = self.GET('key')
      if key:
        act = self.GET('act')
        campus = db.get(key)
        js = "var dom = 'map'; var json = " + campus.json + ";";
        temp ={'js': js}
        if act == "preview":
          self.RENDER('campus_gen_preview.html', temp)
          return
        if act == "code":
          self.RENDER('campus_gen_code.html', temp)
          return
        if act == "iframe":
          self.RENDER('campus_gen_iframe.html', temp)
          return
        return
      self.RENDER('campus.html', {})
